package designmodel.builder.简化建造者模式;

import java.util.StringJoiner;

/**
 *
 *
 * @author scylla
 * @version V1.0
 * @project echo-java-base
 * @title
 * @package designmodel.builder.建造者模式
 * @since 2021/12/15 4:47 下午
 */
public class Computer {
    //必须
    private String cpu;
    //必须
    private String ram;
    //可选
    private int usbCount;
    //可选
    private String keyboard;
    //可选
    private String display;

    public Computer(Builder builder) {
        this.cpu = builder.cpu;
        this.ram = builder.ram;
        this.usbCount = builder.usbCount;
        this.keyboard = builder.keyboard;
        this.display = builder.display;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Computer.class.getSimpleName() + "[", "]")
                .add("cpu='" + cpu + "'")
                .add("ram='" + ram + "'")
                .add("usbCount=" + usbCount)
                .add("keyboard='" + keyboard + "'")
                .add("display='" + display + "'")
                .toString();
    }

    static class Builder {

        //必须
        private String cpu;
        //必须
        private String ram;
        //可选
        private int usbCount;
        //可选
        private String keyboard;
        //可选
        private String display;

        public Builder(String cpu, String ram) {
            this.cpu = cpu;
            this.ram = ram;
        }

        public Builder usbCount(int usbCount) {
            this.usbCount = usbCount;
            return this;
        }

        public Builder keyboard(String keyboard) {
            this.keyboard = keyboard;
            return this;
        }

        public Builder display(String display) {
            this.display = display;
            return this;
        }

        public Computer build() {
            return new Computer(this);
        }
    }

    public static void main(String[] args) {
        Computer computer = new Builder("AMD", "道奇")
                .usbCount(2)
                .keyboard("雷蛇键盘")
                .display("AOC显示器")
                .build();

        System.out.println(computer);
    }
}
